package com.soft.javase.day06.regex;

import java.util.Scanner;

/**
 * @author yzh
 * @desc TODO
 * @date 2025/4/24
 */
public class CheckPasswordStrength {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入密码进行强度检测：");
        String password = sc.nextLine();

        String strength = checkPasswordStrength(password);
        System.out.println("密码强度: " + strength);
    }

    public static String checkPasswordStrength(String password) {
        if (password == null || password.isEmpty()) {
            return "无密码";
        }

        // 弱密码：只有数字或只有字母，且长度<8
        if ((password.matches("^\\d+$") || password.matches("^[a-zA-Z]+$"))) {
            return password.length() < 8 ? "弱" : "中等";
        }

        // 中等密码：包含数字和字母，长度≥8
        if (password.matches("^[a-zA-Z0-9]{8,}$")) {
            return "中等";
        }

        // 强密码：包含数字、字母和特殊字符，长度≥8
        if (password.matches("^[\\w\\W]{8,}$") &&
                password.matches(".*[0-9].*") &&
                password.matches(".*[a-zA-Z].*") &&
                password.matches(".*[^a-zA-Z0-9].*")) {
            return "强";
        }

        // 其他情况
        return password.length() < 8 ? "弱" : "中等";
    }
}
